package com.sskj.gcc.ui.fragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hey.lib.HeySpinner;
import com.sskj.gcc.R;
import com.sskj.gcc.presenter.PayIn1FragmentPresenter;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.m.bean.AseetListBean;
import com.sskj.gocoin.m.bean.RechargeInfoBean;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.CopyUtil;
import com.sskj.gocoin.utils.ImgUtils;
import com.sskj.gocoin.utils.QRCodeUtil;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.activity.RechargeActivity;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.APP_FRAGMENT_PAY_IN1)
public class PayIn1Fragment extends BaseFragment<PayIn1FragmentPresenter> {
    @BindView(R2.id.iv_qr_code)
    ImageView ivQrCode;
    @BindView(R2.id.tv_save_qr)
    TextView tvSaveQr;
    @BindView(R2.id.tv_code)
    TextView tvCode;
    @BindView(R2.id.tv_copy)
    TextView tvCopy;
    @BindView(R2.id.recharge_hint)
    TextView mRechargeHint;
    Bitmap mbitmap;
    @BindView(R2.id.tv1)
    TextView tv1;
    @BindView(R2.id.tv3)
    TextView tv3;
    @BindView(R2.id.toolbar_left)
    TextView toolbarLeft;
    @BindView(R2.id.toolbar_center)
    HeySpinner toolbarCenter;

    private List<AseetListBean.AseetBean> assetBeans = new ArrayList();
    private String assetName;
    private int assetId;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pay_in1;
    }

    @Override
    public PayIn1FragmentPresenter getPresenter() {
        return new PayIn1FragmentPresenter();
    }

    private void initSpinner() {
        List<String> data = new ArrayList<>();
        for (AseetListBean.AseetBean bean : assetBeans) {
            data.add(bean.pname);
        }
        toolbarCenter.attachData(data);
        toolbarCenter.setOnSelectListener(position -> {
            assetName = assetBeans.get(position).pname;
            assetId = assetBeans.get(position).pid;
            String str = App.INSTANCE.getString(com.sskj.gocoin.R.string.cbxz) + "：\r\n"
                    + App.INSTANCE.getString(com.sskj.gocoin.R.string.strRechargeActivity203) + assetName + App.INSTANCE.getString(com.sskj.gocoin.R.string.strRechargeActivity204) + assetName + App.INSTANCE.getString(com.sskj.gocoin.R.string.strRechargeActivity205) + assetName + App.INSTANCE.getString(com.sskj.gocoin.R.string.strRechargeActivity206) + assetName + App.INSTANCE.getString(com.sskj.gocoin.R.string.strRechargeActivity2051);
            tv1.setText(str);
            tvCode.setText("");
            ivQrCode.setImageBitmap(null);
            mRechargeHint.setText(App.INSTANCE.getString(com.sskj.gocoin.R.string.strRechargeActivity208));
            tv3.setText(App.INSTANCE.getString(R.string.strRechargeActivity222));
            mPresenter.recharge(assetId);
        });
        toolbarCenter.setSelectIndex(0);
    }

    @Override
    public void initView() {
        mPresenter.requestUserAssetList();
    }

    public void updateView(RechargeInfoBean bean) {
        tvCode.setText(bean.getAddress());
        QRCodeUtil.createImage(bean.getQrcode(), (int) RudenessScreenHelper.pt2px(getActivity(), 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                mbitmap = bitmap;
                ivQrCode.setImageBitmap(bitmap);
            }

            @Override
            public void onAnalyzeFailed() {

            }
        });
        ClickUtil.click(tvCopy, () -> {
            CopyUtil.copy(tvCode.getText().toString());
        });
        ClickUtil.click(tvSaveQr, () -> {
            savePic();

        });
        ivQrCode.setOnLongClickListener(v -> {
            savePic();
            return true;
        });

    }


    public void savePic() {
        new RxPermissions(getActivity())
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        if (ImgUtils.saveImageToGallery(getActivity(), ivQrCode)) {
                            ToastUtil.showShort(getString(R.string.app_payIn1Fragment2));
                        }
                    }
                });
    }

    public static void start(Context context, List<AseetListBean.AseetBean> data) {
        Intent intent = new Intent(context, RechargeActivity.class);
        intent.putExtra("data", (Serializable) data);
        context.startActivity(intent);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RechargeActivity.class);
//        intent.putExtra("data", (Serializable) data);
        context.startActivity(intent);
    }

    public void onDataSuccess(AseetListBean data) {
        Flowable.fromIterable(data.asset)
                .filter(assetBean -> !assetBean.pname.equals("GCC"))
                .toList()
                .subscribe(assetBeans -> {
                    this.assetBeans = assetBeans;
                    initSpinner();
                });
    }
}
